package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.Crowd;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 检验 根据人群习惯查询检验项目
public class ItemListByHabitActivity extends Base2Activity {
    private RecyclerView rcv_life_habit, rcv_item;// 生活习惯

    private LifeHabitListAdapter adapter;
    private List<Crowd> crowdList = new ArrayList<>();// 传过来的习惯列表
    private int adapt;// 传过来的adapt

    private ItemListAdapter itemAdapter;
    private List<ItemSet> itemList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_item_list_by_habit;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_life_habit = findViewById(R.id.rcv_life_habit);
        rcv_item = findViewById(R.id.rcv_item);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            crowdList = (List<Crowd>) bundle.getSerializable("crowdList");
            adapt = bundle.getInt("adapt");
        }

        setHeadTitle("检验");

        adapter = new LifeHabitListAdapter(this);
        adapter.type = 2;
        GridLayoutManager manager2 = new GridLayoutManager(this, 3);
        rcv_life_habit.setLayoutManager(manager2);
        rcv_life_habit.setAdapter(adapter);
        adapter.setDataList(crowdList);

        itemAdapter = new ItemListAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_item.setLayoutManager(manager);
        rcv_item.setAdapter(itemAdapter);

        hideProgress();
        reqItemListByHabit();
        showProgress();

        setListeners();
    }

    private void setListeners() {
        itemAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", itemList.get(position).getId());
                bundle.putString("type", "1");
                startActivity(ItemSetDetailActivity.class, bundle);
            }
        });
    }

    private Disposable reqItemListByHabit() {
        List<String> crowdIds = new ArrayList<>();
        for (Crowd crowd : crowdList) {
            crowdIds.add(crowd.getId());
        }
        String[] crowdIdsArray = new String[crowdIds.size()];
        crowdIds.toArray(crowdIdsArray);
        return APIRetrofit.getDefault().reqItemListByHabit(crowdIdsArray, adapt, "", 1, 999999)
                .compose(RxUtil.<BaseResult2<List<ItemSet>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ItemSet>>>() {
                    @Override
                    public void accept(BaseResult2<List<ItemSet>> listResult2) throws Exception {
                        hideProgress();
                        if (listResult2.isSuccess()) {

                        } else {

                        }
                        itemList = listResult2.getData();
                        itemAdapter.setDataList(itemList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class LifeHabitListAdapter extends ListBaseAdapter<Crowd> {

        public int type = 1;// 1 选择的习惯 2 所有的习惯

        public LifeHabitListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_habit_sel_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            ImageView iv_del = holder.getView(R.id.iv_del);

            Crowd crowd = mDataList.get(position);
            tv_name.setText(crowd.getName());

            if (type == 1) {
                if (position == 0) {
                    iv_del.setVisibility(View.GONE);
                } else {
                    iv_del.setVisibility(View.VISIBLE);
                }
            } else {
                iv_del.setVisibility(View.GONE);
            }
        }
    }

    private class ItemListAdapter extends ListBaseAdapter<ItemSet> {

        public ItemListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_item_list_by_habit;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_day = holder.getView(R.id.tv_day);
            TextView tv_pe1 = holder.getView(R.id.tv_pe1);
            TextView tv_pe2 = holder.getView(R.id.tv_pe2);
            TextView tv_pe3 = holder.getView(R.id.tv_pe3);
            TextView tv_pe4 = holder.getView(R.id.tv_pe4);
            TextView tv_pe5 = holder.getView(R.id.tv_pe5);
            TextView tv_purpose = holder.getView(R.id.tv_purpose);

            ItemSet item = mDataList.get(position);
            tv_name.setText(item.getName());
            tv_price.setText("¥" + item.getPrice());
            tv_day.setText(item.getDay() + "个工作日");
            tv_purpose.setText(item.getSignificance());

            List adapt = item.getAdapt();
            if (adapt.contains("1")) {
                tv_pe1.setVisibility(View.VISIBLE);
            } else {
                tv_pe1.setVisibility(View.GONE);
            }
            if (adapt.contains("2")) {
                tv_pe2.setVisibility(View.VISIBLE);
            } else {
                tv_pe2.setVisibility(View.GONE);
            }
            if (adapt.contains("3")) {
                tv_pe3.setVisibility(View.VISIBLE);
            } else {
                tv_pe3.setVisibility(View.GONE);
            }
            if (adapt.contains("4")) {
                tv_pe4.setVisibility(View.VISIBLE);
            } else {
                tv_pe4.setVisibility(View.GONE);
            }
            if (adapt.contains("5")) {
                tv_pe5.setVisibility(View.VISIBLE);
            } else {
                tv_pe5.setVisibility(View.GONE);
            }
        }
    }
}